package com.lovo.controller;

import com.lovo.bean.CommentBean;
import com.lovo.bean.PostBean;
import com.lovo.bean.UserBean;
import com.lovo.sercive.ICommentService;
import com.lovo.sercive.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private ICommentService service;

    @RequestMapping("findAll")
    public List<CommentBean> findAllCommentByPostId(int id) {
        return service.findAllCommentByPostId(id);
    }

    @RequestMapping("add")
    public String add(CommentBean comment,int postId, HttpServletRequest req) {
        UserBean user=(UserBean) req.getSession().getAttribute("loginUser");
        if (user!=null){
            comment.setUser(user);
            PostBean post = new PostBean();
            post.setId(postId);
            comment.setPostBean(post);
            service.addComment(comment);
            return "ok";
        }else {
            return "no";
        }
    }
}
